import { IRepoDetail } from '@models/repo';
import { Schema, model } from 'mongoose';

export interface IApp {
  _id?: Schema.Types.ObjectId;
  createdAt?: Date;
  name: string; // 应用名称
  remark: string; // 应用描述
  repository: Schema.Types.ObjectId[]; // 应用对应的仓库ID
  scriptGroup: Schema.Types.ObjectId[]; // 应用脚本组
  updatedAt?: Date;
}

export type IAppDetails = {
  repository: IRepoDetail[];
} & IApp;

export const AppModel = model<IApp>(
  'App',
  new Schema<IApp>(
    {
      name: { required: true, type: String },
      remark: { type: String },
      repository: { default: [], ref: 'RepoModel', type: [Schema.Types.ObjectId] },
      scriptGroup: { default: [], type: [Schema.Types.ObjectId] },
    },
    {
      // 自动添加createdAt和updatedAt时间戳
      timestamps: true,
      toObject: {
        getters: true, // 包含虚拟字段
        minimize: false, // 包含空对象
        versionKey: false, // 不包含 __v 版本键
        virtuals: true, // 包含虚拟属性
      },
    },
  ),
);
